<template>
  <div class="myMessagesContainer">
    <el-table
      :data="tableData"
      style="width: 100%"
      border
      stripe
      :max-height="maxheight"
    >
      <el-table-column prop="body" label="通知内容" fixed>
        <template slot-scope="scope">
          <div
            style="
              white-space: nowrap;
              overflow: hidden;
              text-overflow: ellipsis;
            "
          >
            {{ scope.row.body }}
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="is_read" label="状态" width="180">
        <template slot-scope="scope">
          {{ scope.row.is_read === "0" ? "未读" : "已读" }}
        </template>
      </el-table-column>
      <el-table-column prop="create_time" label="通知时间" width="180">
        <template slot-scope="scope">
          {{
            new Date(Number(scope.row.create_time) * 1000)
              .toLocaleString()
              .replace(/:\d{1,2}$/, " ")
          }}
        </template></el-table-column
      >
      <el-table-column label="操作" fixed="right" width="120">
        <template slot-scope="scope">
          <el-button
            type="text"
            size="small"
            @click="showDetailDialog(scope.row)"
            >详情</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <div class="operation">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import { reqMyMessagesList, reqMyMessagesDetail } from "@/api/message";
export default {
  name: "myMessages",
  data() {
    return {
      maxheight: "auto",
      tableData: [],
      currentPage: 1,
      pageSize: 20,
      total: 0,
    };
  },
  mounted() {
    // 获取数据并更新表格
    this.getList();
  },
  methods: {
    async getList() {
      let res = await reqMyMessagesList({
        page: this.currentPage + "",
        page_size: this.pageSize + "",
      });
      if (res.code === 10000) {
        this.tableData = res.data.data || [];
        if (this.tableData.length > 7) {
          //无操作栏
          this.maxheight = document.documentElement.clientHeight - 205;
        } else {
          this.maxheight = "auto";
        }
        this.currentPage = res.data.pagination.page * 1;
        this.pageSize = res.data.pagination.page_size * 1;
        this.total = res.data.pagination.total * 1;
      }
    },
    async showDetailDialog(val) {
      let res = await reqMyMessagesDetail({
        message_id: val.id,
      });
      if (res.data.jump_link) {
        this.$confirm(res.data.body, "消息通知", {
          cancelButtonText: "关闭",
          confirmButtonText: "去处理",
          customClass: "dialogClass",
        })
          .then(() => {
            this.getList();
            window.open(res.data.jump_link, "_blank");
          })
          .catch(() => {
            this.getList();
          });
      } else {
        this.$alert(res.data.body, "消息通知", {
          confirmButtonText: "关闭",
          customClass: "dialogClass",

          callback: (action) => {
            this.getList();
          },
        });
      }
    },
    handleCurrentChange(page) {
      // 获取数据
      this.currentPage = page;
      this.getList();
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.getList();
    },
  },
};
</script>

<style lang="less" scoped>
.dialogClass {
  p {
    word-wrap: break-word;
  }
}
.myMessagesContainer {
  padding: 10px 15px;
  background-color: #fff;
  // height: 80vh;
  .operation {
    padding-top: 15px;
    // height: 40px;
    .el-button {
      margin-left: 0px;
      margin-right: 10px;
    }
    ::v-deep .el-pagination {
      // float: right;
      text-align: end;
      margin: 0px 0px 0px 20px;
      .el-input--mini .el-input__inner {
        height: 24px;
      }
    }
  }
}
</style>